package com.llvision.myvitualapkdemo;

import android.Manifest;
import android.content.Intent;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.didi.virtualapk.PluginManager;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = (TextView) findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast toast = Toast.makeText(MainActivity.this, "haha", Toast.LENGTH_SHORT);
                toast.show();
                // Given "com.didi.virtualapk.demo" is the package name of plugin APK,
                // and there is an activity called `MainActivity`.
                Intent intent = new Intent();
                intent.setClassName("com.llvision.plugindemo", "com.llvision.plugindemo.PluginMainActivity");
                startActivity(intent);
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        Toast.makeText(this, "有权限了", Toast.LENGTH_SHORT).show();
        File file = new File("/sdcard/Test.apk");
        try {
            file.deleteOnExit();
            final InputStream open = getAssets().open("com.llvision.plugindemo.apk");
            final int available = open.available();
            byte[] bytes = new byte[available];
            open.read(bytes);
            open.close();
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes);
            fileOutputStream.flush();
            fileOutputStream.close();
            PluginManager.getInstance(this).loadPlugin(file);
            Toast toast = Toast.makeText(this, "加载插件完成", Toast.LENGTH_SHORT);
                        toast.show();

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("Host","IoException",e);
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Host","Exception",e);

            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();

        }
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
